package leetcode.editor.cn.q1_300.q50;

import java.util.Arrays;

/**
 * 把 0 都放到前面，2 都放到后面
 *
 * @author qidi
 * @date 2021-12-16 10:54:54
 */
class Q0075_SortColors {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public void sortColors(int[] nums) {
            if (nums.length == 1) return;

            int l = 0;
            int r = nums.length - 1;
            for (int i = 0; i <= r; ) {
                if (nums[i] == 0) {
                    nums[i] = nums[l];
                    nums[l] = 0;
                    l++;
                    i++;
                } else if (nums[i] == 2) {
                    nums[i] = nums[r];
                    nums[r] = 2;
                    r--;
                } else {
                    i++;
                }
            }
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0075_SortColors().new Solution();
        int[] arr;

        arr = new int[]{2, 0, 1};
        solution.sortColors(arr);
        System.out.println(Arrays.toString(arr));

        arr = new int[]{2, 0, 2, 1, 1, 0};
        solution.sortColors(arr);
        System.out.println(Arrays.toString(arr));

        arr = new int[]{2};
        solution.sortColors(arr);
        System.out.println(Arrays.toString(arr));
    }
}